package DAO;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import model.Subject;
import util.DataUtil;

public class SubjectStaffDAO {

    public static boolean addSubjectStaff(String staffID, List<String> SubjectID) {
        int kq = 0;
        int[] ck = new int[SubjectID.size()];
        String call = "{Call ADD_SUBJECT_STAFF(?,?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            for (String s : SubjectID) {
                cs.setString(1, staffID);
                cs.setString(2, s);
                cs.addBatch();
            }
            cs.executeBatch();
            for (int i = 0; i < ck.length; i++) {
                if (ck[i] < 1) {
                    kq = 1;
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return (kq > 0) ? true : false;
    }

    public static List<Subject> getSubjectByStaff(String StaffID) {
        List<Subject> list = new ArrayList<Subject>();
        String call = "{Call GET_SUBJECT_BY_STAFF(?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, StaffID);
            ResultSet rs = cs.executeQuery();
            while (rs.next()) {
                Subject s = new Subject();
                s.setSubjectId(rs.getString("SubjectID"));
                s.setSubjectName(rs.getString("SubName"));
                list.add(s);
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return list;
    }

    public static boolean deleteSubjectStaff(String StaffID) {
        int rs = 0;
        String call = "{Call DELETE_SUBJECT_STAFF(?)}";
        try {
            CallableStatement cs = DataUtil.getConnection().prepareCall(call);
            cs.setString(1, StaffID);
            rs = cs.executeUpdate();
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
        return (rs > 0) ? true : false;
    }
}
